package com.clubs.coding.Controller;

import cn.hutool.json.JSONObject;
import com.clubs.coding.Service.ConstantAdminService;
import com.clubs.coding.Service.NewsService;
import com.clubs.coding.common.ApiRestResponse;
import com.clubs.coding.model.dao.*;
import com.clubs.coding.model.pojo.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@ResponseBody
@RequestMapping("/admin/constant")

public class ConstantAdminController {
@Autowired
    ConstantAdminService constantAdminService;
    @Autowired
    ClassifyLinkmanMapper classifyLinkmanMapper;
    @Autowired
    ClassifyRecordMapper classifyRecordMapper;
    @Autowired
    ClassifyTimeMapper classifyTimeMapper;
    @Autowired
    ClassifyTimeTableMapper classifyTimeTableMapper;
    @Autowired
    ClassifyUsersMapper classifyUsersMapper;
//获取所有联系方式
    @GetMapping("/linkman/{key}")
    public ApiRestResponse linkman(@PathVariable(value ="key",required = false) Integer key,@RequestParam(value = "id", required = false )Integer id,@RequestParam(value = "wx", required = false )String wx,@RequestParam(value = "phone", required = false )String phone){
        if (key ==1) {
            List<ClassifyLinkman> linkman = constantAdminService.linkman();
            return ApiRestResponse.success(linkman);
        }
        if(key==2){

            ClassifyLinkman classifyLinkman = new ClassifyLinkman();
            classifyLinkman.setId(id);
            if (wx!=null) {
                classifyLinkman.setLinkmanWx3(wx);
            }
            if (phone!=null) {
                classifyLinkman.setLinkmanPhone3(phone);
            }

            classifyLinkmanMapper.updateByPrimaryKeySelective(classifyLinkman);
            return ApiRestResponse.success();
        }
       return ApiRestResponse.error(300,"请检传产");


    }
//扣除资费的查询与修改
    @GetMapping("/record")
    public ApiRestResponse record(Integer id,Integer RestRecord,Integer RefilCard,Integer type){
        System.out.println(type);
        if (type==1) {//查询
            List<ClassifyRecord> classifyRecords = classifyRecordMapper.selectAll();
            return ApiRestResponse.success(classifyRecords);
        }else{
            ClassifyRecord classifyRecord = classifyRecordMapper.selectByPrimaryKey(id);
            classifyRecord.setId(id);

            if (RestRecord!=null) {
                classifyRecord.setRestRecord(RestRecord);
            }
            if (RefilCard!=null) {
                classifyRecord.setRefillCard(RefilCard);
            }
            classifyRecordMapper.updateByPrimaryKeySelective(classifyRecord);
            return ApiRestResponse.success();
        }




    }
    @PostMapping("/time")
    //时间产量的修改和读取与增加
 public ApiRestResponse time(Integer id, String time,Integer type){
     ClassifyTime classifyTime = new ClassifyTime();
     if (type == 1) {
         List<ClassifyTime> classifyTimes = classifyTimeMapper.selectAll();
         return ApiRestResponse.success(classifyTimes);
     }else if (type == 2){
         classifyTime.setTiem4(time);
         classifyTimeMapper.insertSelective(classifyTime);
         return ApiRestResponse.success("新增成功");
     }else {
         classifyTime.setId(id);
         classifyTime.setTiem4(time);
         int i = classifyTimeMapper.updateByPrimaryKeySelective(classifyTime);
         return ApiRestResponse.success("修改成功");
     }
 }
 @PostMapping("/tiemTable")
 //常用项目管理常量的CURD
    public ApiRestResponse tiemTable(Integer id,Integer type, String project_name_1,String target_1,String site_1){
        ClassifyTimeTable timeTable = new ClassifyTimeTable();
        if (project_name_1 !=null) {
            timeTable.setProjectName1(project_name_1);
        }
        if (target_1 !=null) {
            timeTable.setTarget1(target_1);
        }
        if (site_1!=null) {
            timeTable.setSite1(site_1);
        }
        if (type==1) {//查
            List<ClassifyTimeTable>  classifyTimeTables = classifyTimeTableMapper.selectAll();
            return ApiRestResponse.success(classifyTimeTables);
        }else if(type==2){//增
            classifyTimeTableMapper.insertSelective(timeTable);
            return ApiRestResponse.success();
        }else if(type ==3 ){//改
            timeTable.setId(id);
            classifyTimeTableMapper.updateByPrimaryKeySelective(timeTable);
            return ApiRestResponse.success();
        }else {//删除
            timeTable.setId(id);
            classifyTimeTableMapper.deleteByPrimaryKey(id);
            return ApiRestResponse.success();
        }

    }
    //组别的CURD
    @PostMapping("/CurdUser")
    public ApiRestResponse CurdUser(Integer type,Integer id,String name){
        ClassifyUsers classifyUsers = new ClassifyUsers();
        if (name !=null) {
            classifyUsers.setUsersClassifying2(name);
        }

        if (type ==1) {
            List<ClassifyUsers> classifyUsers1 = classifyUsersMapper.selectAll();
            return ApiRestResponse.success(classifyUsers1);
        }else if(type==2){//增
            classifyUsersMapper.insertSelective(classifyUsers);
            return ApiRestResponse.success();
        }else if(type==3){
            classifyUsers.setId(id);
            classifyUsersMapper.updateByPrimaryKeySelective(classifyUsers);
            return ApiRestResponse.success();
        }else{
            classifyUsersMapper.deleteByPrimaryKey(id);
            return ApiRestResponse.success();
        }

    }
    /**
    * 数据分析
    * */
    @PostMapping("/dataAnalysis")
    public ApiRestResponse dataAnalysis(){
        String list= constantAdminService.dataAnalysis();
        return ApiRestResponse.success(list);
    }
}
